package com.shengyuan.manage_os.ui.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.shengyuan.manage_os.R;
import com.shengyuan.manage_os.base.BaseFragment;
import com.shengyuan.manage_os.im.common.CCPAppManager;
import com.shengyuan.manage_os.im.common.dialog.ECAlertDialog;
import com.shengyuan.manage_os.im.common.utils.ToastUtil;
import com.shengyuan.manage_os.im.ui.settings.SettingsActivity;
import com.shengyuan.manage_os.im.ui.settings.WebViewUI;
import com.shengyuan.manage_os.old.ying.erpm.InfomationActivity;
import com.shengyuan.manage_os.old.ying.erpm.LoginActivity;
import com.shengyuan.manage_os.old.ying.erpm.util.SharePreferenceUtil;
import com.shengyuan.manage_os.sqlite.SQLiteTools;
import com.shengyuan.manage_os.ui.AddFirendActivity;
import com.shengyuan.manage_os.view.CircleImageView;
import com.shengyuan.manage_os.view.MyListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oa.database.bean.Employee;

/**
 * Created by Administrator on 2016/11/17.
 */

public class MyselfFragment extends BaseFragment implements AdapterView.OnItemClickListener,View.OnClickListener{

    private MyselfFragment myselfFragment;
    private MyListView msg_list;
    private SimpleAdapter sim_adapter;
    private RelativeLayout info_user;
    private TextView groupName,userName;
    private CircleImageView hear;
    private MessageFragment M;
    private byte[] hearData;
    private List<Map<String,Object>> data_list = new ArrayList<>();
    private ImageView relativeLayout;

    private int[] icon = {
            R.mipmap.user_my_redpackets_icon,
            R.mipmap.security,
            R.mipmap.default_org_icon,
            R.mipmap.teleconf_voip_hangup_icon,
            R.mipmap.my_recommend_icon,
            R.mipmap.my_setting_icon};
    private String[] iconName = { "工资", "社保", "培训", "考勤","工龄",  "设置"};


    public MyselfFragment(){
        super();
        init();
    }

    public  MyselfFragment init(){
//        if(myselfFragment == null){
            transaction = manager.beginTransaction();
            myselfFragment = this;
            transaction.add(R.id.fragment_content,myselfFragment);
            transaction.commit();
//        }
        return myselfFragment;
    }
    View view = null;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        if(view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if(parent != null) {
                parent.removeView(view);
            }
        }

        view = inflater.inflate(R.layout.fragment_myself,null);
        msg_list = (MyListView) view.findViewById(R.id.list_myself);
        hear = (CircleImageView) view.findViewById(R.id.icon);
        info_user = (RelativeLayout) view.findViewById(R.id.info_user);
        info_user.setOnClickListener(this);
        hear.setOnClickListener(this);
        showHearImage();
//        SharePreferenceUtil util = SharePreferenceUtil.init(getActivity(), "SAVE_USER");
        relativeLayout = (ImageView) view.findViewById(R.id.gongsi);
        relativeLayout.setOnClickListener(this);
        groupName = (TextView) view.findViewById(R.id.groupName);
        groupName.setText("江盛源建设有限公司");
        userName = (TextView) view.findViewById(R.id.userName);
        userName.setText(LoginActivity.pmUser.UserName);
        data_list = new ArrayList<>();
        getData();
        String [] from ={"image","text"};
        int [] to = {R.id.image,R.id.text};
        sim_adapter = new SimpleAdapter(this.getActivity(), data_list, R.layout.myself_item, from, to);
        msg_list.setAdapter(sim_adapter);
        msg_list.setOnItemClickListener(this);
        return view;
    }

    public List<Map<String, Object>> getData(){
        //cion和iconName的长度是相同的，这里任选其一都可以
        for(int i=0;i<icon.length;i++){
            Map<String, Object> map = new HashMap<>();
            map.put("image", icon[i]);
            map.put("text", iconName[i]);
            data_list.add(map);
        }

        return data_list;
    }



    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent();
        switch (position){
            case 0:
//                View contentView = View.inflate(getActivity(),R.layout.check_passwork , null);
//                final EditText e = (EditText) contentView.findViewById(R.id.passwork);
//                final ECAlertDialog alertDialog = new ECAlertDialog(getActivity());
//                alertDialog.setContentView(contentView);
//                alertDialog.setButton(ECAlertDialog.BUTTON_NEGATIVE, R.string.app_cancel, null);
//                alertDialog.setButton(ECAlertDialog.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
//
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        if("".equals(e.getText().toString())){
//                            Toast.makeText(getActivity(),"密码不能为空",Toast.LENGTH_LONG).show();
//                        }else {
//                            Toast.makeText(getActivity(),e.getText().toString(),Toast.LENGTH_LONG).show();
//                            alertDialog.dismiss();
//                        }
//
//                    }
//                });
//                alertDialog.show();
                break;
            case 5:
                intent.setClass(getActivity(), SettingsActivity.class);
                startActivity(intent);
                break;
        }

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        if (LoginActivity.pmUser==null|| LoginActivity.pmUser.ID==0 ) {
            intent.setClass( getActivity(), LoginActivity.class );
            intent.putExtra("isFirst",false);
            startActivity(intent);
            return;
        }else if (LoginActivity.pmUser.getLogOut() == true) {
            intent.setClass( getActivity(), LoginActivity.class );
            intent.putExtra("isFirst",false);
            startActivity(intent);
            return;
        }
        switch (v.getId()){
            case R.id.gongsi:
                intent.setClass(getActivity(), WebViewUI.class);
                intent.putExtra("url","http://www.jiangshengyuan.com/");
                startActivity(intent);
                break;
            case R.id.icon:
                intent.setClass(getActivity(), AddFirendActivity.class);
                startActivityForResult(intent,101);
                break;
            case R.id.info_user:
                intent.setClass(getActivity(), InfomationActivity.class);
                startActivity(intent);
                break;

        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if(requestCode == 101&& resultCode == 1000){
            showHearImage();
        }
    }

    private void showHearImage(){
        Employee employee = SQLiteTools.queryAssignEmployee(CCPAppManager.getClientUser().getUserId());
        try {
            hearData = Base64.decode(employee.getHearImage(),Base64.DEFAULT);
            hear.setImageBitmap(BitmapFactory.decodeByteArray(hearData,0,hearData.length));
        }catch (Exception e){
        }
    }
}

